var co = require('co');
var seqModule = require('../../models/index');
var db = new seqModule();
var util = require('../../common/util');
var meetScheduleController = {
    getSchedule: function (req, res) {
        co(function () {
           // var page = parseInt(req.query.page);
            // var params = {};
            // var paramT = {};
            // var paramU = {};
            // if(req.query.openId != undefined){
            //     paramU.openId = req.query.openId;
            // }
            // var include = [{model:db.buildModule,attributes:['buildTitle']},{model:db.meetSubjectModule,attributes:['subjectTitle']},{model:db.meetOrderModule,where:paramU}];
            // paramT.include = include;
            // if (req.query.speakHostId != undefined) {
            //     params["speakHostId"] = req.query.speakHostId;
            // }
            // paramT["where"] = params;
            // paramT["order"] = [["meetDate", 'ASC'],['startTime','ASC']];
            // // if (req.query.page != undefined && req.query.rows != undefined) {
            // //     paramT["offset"] = parseInt(req.query.page);
            // //     paramT["limit"] = parseInt(req.query.rows);
            // // }
            // db.meetScheduleModule.findAll(paramT).then(function (result) {
            //     res.status(200).json(result);
            // });
            var memSql = "";
            if(req.query.openId != undefined){
                memSql += " and open_id = '"+req.query.openId +"'";
            }
            var scheduleSql = "select schedule.id,schedule.schedule_title as scheduleTitle,schedule.name,schedule.start_time as startTime,schedule.end_time as endTime,schedule.meet_time as meetTime,schedule.meet_date as meetDate,schedule.type,"+
            "bud.build_title as buildTitle,subject.subject_title as subjectTitle,(select count(*) from meet_order where meet_schedule_id = schedule.id "+memSql+") as isOrder,(select count(*) from meet_schedule as sche where sche.meet_date = schedule.meet_date and sche.speak_host_id = schedule.speak_host_id) as taskNum "+
            " from meet_schedule as schedule left join build as bud on schedule.build_id = bud.id left join meet_subject as subject on schedule.meet_subject_id = subject.id where 1=1";
            if (req.query.speakHostId != undefined) {
                scheduleSql+=" and schedule.speak_host_id = '"+req.query.speakHostId+"'";
            }

            scheduleSql+= " order by schedule.meet_date ASC,schedule.start_time ASC";
            db.sequelize.query(scheduleSql).spread(function(result){
                res.status(200).json(result);
            })

        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    }
}
module.exports = meetScheduleController;